package com.liukai.bishe.ui.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.liukai.bishe.R;
import com.liukai.bishe.model.doman.transaction.Transaction;
import com.liukai.bishe.model.doman.transaction.TransactionUseTime;
import com.liukai.bishe.presenter.transactionPresenter.ITransactionPresenter;
import com.liukai.bishe.ui.activity.CountDownActivity;
import com.liukai.bishe.utils.Constants;
import com.liukai.bishe.utils.DateUtils;
import com.liukai.bishe.utils.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.ButterKnife;

public class StopTransactionDialog {

    private final CountDownActivity mCountDownActivity;
    private Context mContext;
    private AlertDialog mDialog = null;
    private TextView mBtnStopCount;
    private TextView mBtnCancel;
    private TextView mBtnCancelCount;
    private String mTime = "0";

    private ITransactionPresenter mITransactionPresenter;

    private TransactionUseTime mTransactionUseTime;

    public StopTransactionDialog(Context context, ITransactionPresenter iTransactionPresenter,
                                 TransactionUseTime transactionUseTime, CountDownActivity countDownActivity) {
        mContext = context;
        this.mITransactionPresenter = iTransactionPresenter;
        this.mTransactionUseTime = transactionUseTime;
        this.mCountDownActivity = countDownActivity;

        View mView_dialog = View.inflate(mContext, R.layout.dialog_stop_transaction,null);
        if (mDialog == null){
            // 创建AlertDialog对象
            mDialog = new AlertDialog.Builder(mContext).create();
            mDialog.show();
            // 设置点击可取消
            mDialog.setCancelable(true);
            // 获取Window对象
            Window window = mDialog.getWindow();
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            // 设置显示视图内容
            window.setContentView(mView_dialog);

//            WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
//            lp.width = 500;// 调整该值可以设置对话框显示的宽度
//            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
//            window.setAttributes(lp);
        }else {
            mDialog.show();
        }

        initView();
        initListener();
    }

    private void initListener() {

        /**
         * 取消计时
         */
        mBtnCancelCount.setOnClickListener(view -> {
            mCountDownActivity.finish();
        });

        /**
         * 结束计时,向服务器发送数据
         */
        mBtnStopCount.setOnClickListener(view -> {
            long timeStamp = System.currentTimeMillis();
            ToastUtil.log(mContext,"当前结束时间戳",timeStamp+"");

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(timeStamp);
            String dateTime = simpleDateFormat.format(date);
            ToastUtil.log(mContext,"当前结束时间",dateTime+"");
            mTransactionUseTime.setStoptime(String.valueOf(timeStamp));
            long totalTime = timeStamp - Long.parseLong(mTransactionUseTime.getStarttime());
            mTransactionUseTime.setStartdate(DateUtils.timeMillisToTime("yyyy-MM-dd",mTransactionUseTime.getStarttime()));

            String timeString = DateUtils.longTimeToMinuties(totalTime);
            if (Long.valueOf(timeString) < 1){
                ToastUtil.show(mContext,"执行1分钟以下的任务，不予计时");
            }else {
                mTransactionUseTime.setThistotaltime(String.valueOf(totalTime));
                // 开启Presenter
                mITransactionPresenter.startTransaction(mTransactionUseTime);
            }
            mCountDownActivity.finish();


        });

        mBtnCancel.setOnClickListener(view -> {
            mDialog.dismiss();
        });

    }

    private void initView() {

        mBtnStopCount = mDialog.findViewById(R.id.btnStopCount);
        mBtnCancel = mDialog.findViewById(R.id.btnCancel);
        mBtnCancelCount = mDialog.findViewById(R.id.btnCancelCount);

    }

    public void show(){
        mDialog.setCanceledOnTouchOutside(false);
//        mDialog.show();
    }


    public void setTime(String string) {
        this.mTime = string;
    }
}
